<?php
/*
stardevelop.com Live Help
International Copyright stardevelop.com

You may not distribute this program in any manner,
modified or otherwise, without the express, written
consent from stardevelop.com

You may make modifications, but only for your own 
use and within the confines of the License Agreement.
All rights reserved.

Selling the code for this program without prior 
written consent is expressly forbidden. Obtain 
permission before redistributing this program over 
the Internet or in any other medium.  In all cases 
copyright and header must remain intact.  
*/
include('../include/spiders.php');
include('../include/database.php');
include('../include/class.mysql.php');
include('../include/config.php');

$login_id = $_REQUEST['ID'];
$username = $_REQUEST['USER'];

header('Content-type: text/html; charset=utf-8');

if (file_exists('../locale/' . LANGUAGE_TYPE . '/guest.php')) {
	include('../locale/' . LANGUAGE_TYPE . '/guest.php');
}
else {
	include('../locale/en/guest.php');
}

?>
<!--
// stardevelop.com Live Help International Copyright 2003
// JavaScript Check Status Functions

var loggingOut = false;
var chatEnded = false;
var currentlyTyping = false;
var initalisedChat = 0;

var MessageTimer;
var LiveHelpXMLHTTP;

function windowLogout() {
	if (loggingOut == false) {
		sendMessageFrame.location.href = './logout.php';
		loggingOut = true;
	}
}

<?php
if ($_SETTINGS['OFFLINEEMAIL'] == true) {
?>
var continueTimer = setTimeout('windowOffline()', <?php echo($guest_login_timeout * 1000); ?>);

function windowOffline() {
	if (self.displayFrame.displayContentsFrame) {
		toggleLayer('ContinueLayer', 'self.displayFrame.displayContentsFrame', true);
	}
}
<?php
}
?>

function currentTime() {
	var date = new Date();
	return date.getTime();
}

function getLayer(ID, frame) {

	var obj;
	if (document.getElementById) {
		if (frame) {
			obj = eval(frame + '.document.getElementById("' + ID + '")');
		} else {
			obj = document.getElementById(ID);
		}
		return obj
	}
	else if (document.layers && document.layers[object] != null) {
		if (frame) {
			obj = eval(frame + '.document.layers["' + ID + '"].document');
		} else {
			obj = document.layers[ID].document;
		}
		return obj;
	}
	else if (document.all) {
		if (frame) {
			obj = eval(frame + '.document.all["' + ID + '"]');
		} else {
			obj = document.all[ID];
		}
		return obj;
	}
	
	return false;
}

function writeLayer(ID, frame, sText) {

	var oLayer = getLayer(ID, frame);
	if (oLayer != false) {
	
		if (oLayer.innerHTML == null) {
			oLayer.open();
			oLayer.write(sText);
			oLayer.close();
		} else {
			oLayer.innerHTML = sText;
		}
	} else {
		return false;
	}

}


function toggleLayer(ID, frame, visibility) {

	var obj = getLayer(ID, frame);
	if (document.layers && obj != null) {
		if (!visibility) {
			obj.visibility = 'hidden';
		} else {
			obj.visibility = 'visible';
		}
	}
	else {
		if (!visibility) {
			obj.style.visibility = 'hidden';
		} else {
			obj.style.visibility = 'visible';
		}
	}	
	return false;
}

function swapImgRestore() {
	var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function preloadImages() { 
	var d=document; if(d.images){ if(!d.p) d.p=new Array();
		var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function findObj(n, d) {
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
		if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
		for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
		if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function swapImage() {
	var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
		if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var ReceivedMessage = -4;
var SentMessage = 0;

function display(id, username, message, align, status) {
	if (self.displayFrame) {
		var output;
		var alignment;
		
		if (align == '1') { alignment = 'left'; }
		else if (align == '2') { alignment = 'center'; }
		else if (align == '3') { alignment = 'right'; }
		
		if (status == '0') { color = '<?php echo($_SETTINGS['SENTFONTCOLOR']); ?>' }
		else { color = '<?php echo($_SETTINGS['RECEIVEDFONTCOLOR']); ?>'; window.focus(); focusChat(); }

		output = '<div align="' + alignment + '" class="message" style="color: '+ color + ';">';

		if (status == '0' || status == '1' || status == '2') { // General Message or Link
			if (username != '') { output += '<strong>' + username + '</strong>: '; }
			message = message.replace(/([a-z0-9][a-z0-9_\.-]{0,}[a-z0-9]@[a-z0-9][a-z0-9_\.-]{0,}[a-z0-9][\.][a-z0-9]{2,4})/g, '<a href="mailto:$1" class="message">$1</a>');
			message = message.replace(/((?:(?:http(?:s?))|(?:ftp)):\/\/[^\s|<|>|'|\"]*)/g, '<a href="$1" target="_blank" class="message">$1</a>');
<?php
if ($_SETTINGS['GUESTSMILIES'] == true) {
?>
			message = htmlSmilies(message);
<?php
}
?>
			output += message;
		} else if (status == '3') { // Image
			message = message.replace(/((?:(?:http(?:s?))):\/\/[^\s|<|>|'|\"]*)/g, '<img src="$1" alt="Received Image">');
			output += message;
		} else if (status == '4') { // PUSH
			eval('if (window.opener) { window.opener.location.href = "' + message + '"; window.opener.focus(); }');
		} else if (status == '5') { // JavaScript
			eval(message);
		} output += '</div>';
		
		var displayFrame = 'self.displayFrame.displayContentsFrame';
		toggleLayer('WaitingLayer', displayFrame);
<?php
if ($_SETTINGS['OFFLINEEMAIL'] == true) {
?>
		toggleLayer('ContinueLayer', displayFrame);
		clearTimeout(continueTimer);
<?php
}
?>
		if (id != '') {
			if (status == 0) { // Visitor
				if (id > SentMessage) {
					writeMessage(output);
					SentMessage = id;
				}
			} else { // Operator
				if (id > ReceivedMessage) {
					writeMessage(output);
					ReceivedMessage = id;
				} else {
				}
			}
		} else {
			writeMessage(output);
		}
	}
}

function writeMessage(output) {

	var displayFrame = 'self.displayFrame.displayContentsFrame';
	var oLayer = getLayer('MessagesLayer', displayFrame);
	if (oLayer != false) {
		if (oLayer.innerHTML == null) {
			writeLayer('MessagesLayer', displayFrame, oLayer + output);
		} else {
			writeLayer('MessagesLayer', displayFrame, oLayer.innerHTML + output);
		}
	} else {
		self.displayFrame.displayContentsFrame.document.write(output);
	}

}

var focussed = false;

function focusChat() {
	if (document.MessageForm.MESSAGE) {
		if (document.MessageForm.MESSAGE.disabled == false && focussed == false) {
			document.MessageForm.MESSAGE.focus();
			var prevText = document.MessageForm.MESSAGE.value;
			document.MessageForm.MESSAGE.value = prevText;
		}
	}
}

function setTyping() {
	if (document['messengerStatus']) { document['messengerStatus'].src = '/livehelp/locale/<?php echo(LANGUAGE_TYPE); ?>/images/user_typing.gif'; }
}

function setWaiting() {
	if (document['messengerStatus']) { document['messengerStatus'].src = '/livehelp/locale/<?php echo(LANGUAGE_TYPE); ?>/images/waiting.gif'; }
}

function refreshDisplayer() {
	window.setTimeout('updateMessages();',<?php echo((int)$chat_refresh_rate * 1000); ?>);
}

function updateMessages() {
	if (self.displayFrame && chatEnded == false) { self.displayFrame.displayRefreshFrame.location.reload(true); }
}

function bottom() {
	if (self.displayFrame) {
		self.displayFrame.displayContentsFrame.window.scrollTo(0, 99999999);
	}
}

function checkXMLHTTP() {
	obj = null;
	if (window.XMLHttpRequest) {
		obj = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		obj = new ActiveXObject("Microsoft.XMLHTTP")
		if (!obj) {
			try {
				obj = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					obj = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					obj = null;
				}
			}
		}
    }
	return obj;
}

function LoadMessagesFrame() {
	if (self.displayFrame && chatEnded == false) {
		//
		self.displayFrame.displayRefreshFrame.document.onload = window.setTimeout('LoadMessagesFrame();', 3000);
		self.displayFrame.displayRefreshFrame.location.href = '/livehelp/refresher.php';
	}
}

LiveHelpXMLHTTP = checkXMLHTTP();

function LoadMessages() {
	
	if (LiveHelpXMLHTTP) {
	
		// Run the XML query
		if (LiveHelpXMLHTTP.readyState != 0) {
			LiveHelpXMLHTTP.abort();
		}
		
		var time = currentTime();
		var URL = '/livehelp/refresher.php?JS=1&TYPING=' + currentlyTyping + '&INIT=' + initalisedChat + '&COOKIE=<?php echo($cookie_domain); ?>&TIME=' + time + '&LANGUAGE=<?php echo(LANGUAGE_TYPE); ?>';
		LiveHelpXMLHTTP.open('GET', URL, true);
		
		LiveHelpXMLHTTP.onreadystatechange = function() {
			if (LiveHelpXMLHTTP.readyState == 4) {
			  // Process response as JavaScript
			  if (LiveHelpXMLHTTP.status == 200) {
			  	eval(LiveHelpXMLHTTP.responseText);
			  }
			}
		};
		
		LiveHelpXMLHTTP.send(null);
		
		// Load the messages again
		MessageTimer = window.setTimeout('LoadMessages();', 3000);
		
	} else {
		LoadMessagesFrame();
	}
	
}

function typing(status) {
	var updateIsTypingStatus = new Image;
	var time = currentTime();
	
	if (LiveHelpXMLHTTP) {
		if (status == true) { status = 1; } else { status = 0; }
		currentlyTyping = status;
	} else {
		if (status == true) {
			var message = document.MessageForm.MESSAGE.value;
			var intLength = message.length;
			if (intLength == 0) {
				typing(false);
			} else {	
				updateTypingStatus.src = '/livehelp/typing.php?ID=<?php echo($login_id); ?>&STATUS=1&TIME=' + time;
			}
		} else {
			updateTypingStatus.src = '/livehelp/typing.php?ID=<?php echo($login_id); ?>&STATUS=0&TIME=' + time;
		}
	}

}

function htmlSmilies(message) {

	var msg = new String();
	msg = message;
		
	msg = msg.replace(/:-\)/g, '<image src="./images/smilie1.gif" alt="Smilie">');
	msg = msg.replace(/:-\(/g, '<image src="./images/smilie2.gif" alt="Smilie">');
	msg = msg.replace(/\$-D/g, '<image src="./images/smilie3.gif" alt="Smilie">');
	msg = msg.replace(/;-P/g, '<image src="./images/smilie4.gif" alt="Smilie">');
	msg = msg.replace(/:-\//g, '<image src="./images/smilie5.gif" alt="Smilie">');
	msg = msg.replace(/:\(/g, '<image src="./images/smilie6.gif" alt="Smilie">');
	msg = msg.replace(/8-\)/g, '<image src="./images/smilie7.gif" alt="Smilie">');
	msg = msg.replace(/:\)/g, '<image src="./images/smilie8.gif" alt="Smilie">');
	msg = msg.replace(/:-\|/g, '<image src="./images/smilie9.gif" alt="Smilie">');
	msg = msg.replace(/:--/g, '<image src="./images/smilie10.gif" alt="Smilie">');
	msg = msg.replace(/\/-\|/g, '<image src="./images/smilie11.gif" alt="Smilie">');
	msg = msg.replace(/:-O/g, '<image src="./images/smilie12.gif" alt="Smilie">');
		
	return msg;
}

function removeHTML(message) {
	
	message = message.replace(/</g, '&lt;');
	message = message.replace(/>/g, '&gt;');
	message = message.replace(/\r\n|\r|\n/g, '<br />');
	return message

}

//-->